//
//  LoadingView.m
//  BaseProject
//
//  Created by zhangrongbing on 2016/11/17.
//  Copyright © 2016年 lovcreate. All rights reserved.
//

#import "LoadingView.h"
#import "UIView+Addition.h"

@interface LoadingView(){
    UITableViewCellSeparatorStyle tableViewOldUITableViewCellSeparatorStyle;
    UIView* supperView;
}

@end

@implementation LoadingView

-(instancetype) initWithView:(UIView *)view{
    NSAssert(view, @"View must not be nil.");
    supperView = view;
    //判断是不是uitableview
    if ([supperView isKindOfClass:[UITableView class]]) {
        UITableView* tableView = (UITableView*)supperView;
        tableViewOldUITableViewCellSeparatorStyle = tableView.separatorStyle;
        [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        tableView.scrollEnabled = NO;
    }
    return [self initWithFrame:view.bounds];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        // 设置默认值
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        self.autoMoveDown = YES;
    }
    return self;
}

//赖加载
-(UIButton*)retryButton{
    if (!_retryButton) {
        _retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_retryButton.titleLabel setFont:[UIFont systemFontOfSize:13.f]];
        _retryButton.backgroundColor = [UIColor whiteColor];
        [_retryButton.layer setShadowColor:[UIColor lightGrayColor].CGColor];
        [_retryButton.layer setShadowOffset:CGSizeMake(10, 10)];
        [_retryButton setTitleColor:[UINavigationBar appearance].barTintColor forState:UIControlStateNormal];
        [_retryButton.layer setBorderWidth:.5];
        [_retryButton.layer setBorderColor:[UINavigationBar appearance].barTintColor.CGColor];
        [_retryButton.layer setMasksToBounds:YES];
        [_retryButton.layer setCornerRadius:2];
        [_retryButton setTitle:_retryMessage forState:UIControlStateNormal];
        [_retryButton addTarget:self action:@selector(pressRetryButton:) forControlEvents:UIControlEventTouchUpInside];
        [_retryButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_retryButton sizeToFit];
    }
    return _retryButton;
}

-(UIImageView*)loadingImageView{
    if (!_loadingImageView) {
        _loadingImageView = [[UIImageView alloc] initWithImage:_loadingImage];
    }
    return _loadingImageView;
}

-(UIImageView*)retryImageView{
    if (!_retryImageView) {
        _retryImageView = [[UIImageView alloc] initWithImage:_retryImage];
    }
    return _retryImageView;
}

-(UILabel*)loadingLabel{
    if (!_loadingLabel) {
        _loadingLabel = [UILabel new];
        _loadingLabel.text = _loadingMessage;
        [_loadingLabel sizeToFit];
    }
    return _loadingLabel;
}

-(void)hide{
    if ([supperView isKindOfClass:[UITableView class]]) {
        UITableView* tableView = (UITableView*)supperView;
        [tableView setSeparatorStyle:tableViewOldUITableViewCellSeparatorStyle];
        tableView.scrollEnabled = YES;
    }
    [self removeFromSuperview];
}

-(void)showWithLoadingImage:(UIImage*)loadingImage message:(NSString*)message{
    _loadingImage = loadingImage;
    _loadingMessage = message;
    //移除重试控件
    if (_retryImage) {
        [_retryImageView removeFromSuperview];
    }
    if (_retryMessage) {
        [_retryButton removeFromSuperview];
    }
    //添加加载控件
    [self addSubview:self.loadingImageView];
    if (message && message.length > 1) {
        [self addSubview:self.loadingLabel];
    }
    [supperView addSubview:self];
}

-(void)showWithRetryImage:(UIImage*)image message:(NSString*) message retryBlock:(retryBlock) block{
    _block = block;
    _retryMessage = message;
    _retryImage = image;
    
    //移除加载控件
    [_loadingImageView removeFromSuperview];
    [_loadingLabel removeFromSuperview];
    
    //添加重试控件
    [self addSubview:self.retryImageView];
    [self addSubview:self.retryButton];
}

//回调块
-(void)pressRetryButton:(UIButton*)button{
    if (_block) {
        _block();
    }
    
    //移除重试控件
    if (_retryImage) {
        [_retryImageView removeFromSuperview];
    }
    if (_retryMessage) {
        [_retryButton removeFromSuperview];
    }
    //添加加载控件
    [self addSubview:self.loadingImageView];
    if (_loadingMessage && _loadingMessage.length > 1) {
        [self addSubview:self.loadingLabel];
    }
    [supperView addSubview:self];
    
}

-(void)layoutSubviews{
    [super layoutSubviews];
    //设置loadingImageView;
    CGFloat offset = _autoMoveDown?64:0;
    _loadingImageView.lc_center = CGPointMake(self.lc_w/2, self.lc_h/2-offset);
    _loadingLabel.lc_center = CGPointMake(self.lc_w/2, CGRectGetMaxY(_loadingImageView.frame));
    
    _retryImageView.lc_center = CGPointMake(self.lc_w/2, self.lc_h/2-offset);
    _retryButton.lc_w = self.lc_w/3;
    _retryButton.lc_center = CGPointMake(self.lc_w/2, CGRectGetMaxY(_retryImageView.frame)+17);
}
@end
